package com.woniu.product.listener;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.rabbitmq.client.Channel;
import com.woniu.product.pojo.TShoppingcart;
import com.woniu.product.service.TShoppingcartService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.woniu.common.constant.MqConstant.Queue.ROLLBACK_SHOPPINGCART_QUEUE;
import static com.woniu.common.constant.MqConstant.Queue.SHOPPINGCART_QUEUE;

/**
 * @author 杜梅杰
 * @description
 * @date 2024/5/13 17:09
 */
@Component
@Slf4j
public class ShoppingcartListener {
    @Autowired
    private TShoppingcartService shoppingcartService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @RabbitListener(queues = SHOPPINGCART_QUEUE)
    @SneakyThrows
    public void updateSku(String msg, Message message, Channel channel) {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        String messageId = message.getMessageProperties().getMessageId();

        try {
            Boolean delete = redisTemplate.delete("message:" + messageId);
            if (delete) {
                JSONObject jsonObject = JSONObject.parseObject(msg, JSONObject.class);
                //把参数拿出来
                JSONArray jsonArray = jsonObject.getJSONArray("shops");
                List<TShoppingcart> shops = jsonArray.toJavaList(TShoppingcart.class);
                //修改购物车状态
                for (TShoppingcart shop : shops) {
                    shop.setStatus(1); // 设置新的状态
                }
                shoppingcartService.updateBatchById(shops);
            }
            // 如果没有问题手动签收
            channel.basicAck(deliveryTag,false);
        } catch (Exception e) {
            log.error("updateSku error:{}",e);
            redisTemplate.opsForValue().set(messageId,messageId,30, TimeUnit.MINUTES);
            // 拒绝签收并不保留消息,直接到死信
            channel.basicReject(deliveryTag,false);
        }

    }

    @RabbitListener(queues = ROLLBACK_SHOPPINGCART_QUEUE)
    @SneakyThrows
    public void rollbackSku(String msg, Message message, Channel channel) {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            JSONObject jsonObject = JSONObject.parseObject(msg, JSONObject.class);
            //把参数拿出来
            JSONArray jsonArray = jsonObject.getJSONArray("shops");
            List<TShoppingcart> shops = jsonArray.toJavaList(TShoppingcart.class);
            //修改购物车状态
            for (TShoppingcart shop : shops) {
                shop.setStatus(0); // 设置新的状态
            }
            shoppingcartService.updateBatchById(shops);
        } catch (Exception e) {
            log.error("rollbackSku error:{}",e);
            // 拒绝签收并不保留消息,直接到死信
            channel.basicReject(deliveryTag,false);
        }
    }
}
